package client.status;

import client.MapleCharacter;
import server.life.MapleMonster;
import server.life.MobSkill;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

public class MonsterStatusEffect {
    private MonsterStatus stati;
    private int skill;
    private MobSkill mobskill;
    private boolean monsterSkill;
    private WeakReference<MapleCharacter> weakChr;
    private Integer x;
    private int poisonDamage;
    private boolean reflect;
    private long cancelTime;
    private long dotTime;
    private boolean newpoison;

    public MonsterStatusEffect(MonsterStatus stat, Integer x, int skillId, MobSkill mobskill, boolean monsterSkill) {
        this.weakChr = null;
        this.poisonDamage = 0;
        this.reflect = false;
        this.cancelTime = 0L;
        this.dotTime = 0L;
        this.newpoison = true;
        this.stati = stat;
        this.skill = skillId;
        this.monsterSkill = monsterSkill;
        this.mobskill = mobskill;
        this.x = x;
    }

    public MonsterStatusEffect(MonsterStatus stat, Integer x, int skillId, MobSkill mobskill, boolean monsterSkill, boolean reflect) {
        this.weakChr = null;
        this.poisonDamage = 0;
        this.reflect = false;
        this.cancelTime = 0L;
        this.dotTime = 0L;
        this.newpoison = true;
        this.stati = stat;
        this.skill = skillId;
        this.monsterSkill = monsterSkill;
        this.mobskill = mobskill;
        this.x = x;
        this.reflect = reflect;
    }

    public MonsterStatus getStatus() {
        return this.stati;
    }

    public WeakReference<MapleCharacter> getchr() {
        return this.weakChr;
    }

    public void setX(int x) {
        this.x = Integer.valueOf(x);
    }

    public Integer getX() {
        return this.x;
    }

    public void setValue(MonsterStatus status, Integer newVal) {
        this.stati = status;
        this.x = newVal;
    }

    public int getSkill() {
        return this.skill;
    }

    public MobSkill getMobSkill() {
        return this.mobskill;
    }

    public boolean isMonsterSkill() {
        return this.monsterSkill;
    }

    public void setCancelTask(long cancelTask) {
        this.cancelTime = System.currentTimeMillis() + cancelTask;
    }

    public long getCancelTask() {
        return this.cancelTime;
    }

    public void setnewpoison(boolean s) {
        this.newpoison = s;
    }

    public void setDotTime(long duration) {
        this.dotTime = duration;
    }

    public long getDotTime() {
        return this.dotTime;
    }

    public void setPoisonDamage(int poisonDamage, MapleCharacter chrr) {
        this.poisonDamage = poisonDamage;
        this.weakChr = new WeakReference(chrr);
    }

    public int getPoisonDamage() {
        return this.poisonDamage;
    }

    public boolean shouldCancel() {
        return this.cancelTime > 0L && this.cancelTime <= System.currentTimeMillis();
    }

    public void cancelTask() {
        this.cancelTime = 0L;
    }

    public boolean isReflect() {
        return this.reflect;
    }

    public int getFromID() {
        return (this.weakChr == null || this.weakChr.get() == null) ? 0 : ((MapleCharacter) this.weakChr.get()).getId();
    }

    public void cancelPoisonSchedule(MapleMonster mm) {
        mm.doPoison(this, this.weakChr);
        this.poisonDamage = 0;
        this.weakChr = null;
    }

    public void scheduledoPoison(MapleMonster mon) {
        Timer timer = new Timer(true);
        long time = System.currentTimeMillis();
        MonsterStatusEffect eff = this;
        if (this.newpoison) {
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    if (time + MonsterStatusEffect.this.getDotTime() > System.currentTimeMillis() && mon.isAlive()) {
                        MonsterStatusEffect.this.setnewpoison(false);
                        mon.doPoison(eff, MonsterStatusEffect.this.weakChr);
                    } else {
                        MonsterStatusEffect.this.setnewpoison(true);
                        timer.cancel();
                    }
                }
            };
            timer.schedule(task, 0L, 1000L);
        }
    }

    public static int genericSkill(MonsterStatus stat) {
        switch (stat) {
            case STUN: {
                return 90001001;
            }
            case SPEED: {
                return 90001002;
            }
            case POISON: {
                return 90001003;
            }
            case DARKNESS: {
                return 90001004;
            }
            case SEAL: {
                return 90001005;
            }
            case FREEZE: {
                return 90001006;
            }
            case MAGIC_CRASH: {
                return 1111007;
            }
            case SHOWDOWN: {
                return 4121003;
            }
            case IMPRINT: {
                return 22161002;
            }
            case SHADOW_WEB: {
                return 4111003;
            }
            case VENOMOUS_WEAPON: {
                return 5211004;
            }
            case DOOM: {
                return 2311005;
            }
            case NINJA_AMBUSH: {
                return 4121004;
            }
            default: {
                return 0;
            }
        }
    }
}
